package cn.git.controller;

import cn.git.entity.Product;
import cn.git.mapper.ProductMapper;
import cn.hutool.core.util.IdUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @description: mybatis批量处理controller
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-09-24
 */
@RestController
@RequestMapping("/batch")
public class BatchController {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 批量插入
     *
     * @return
     */
    @GetMapping("/add/product")
    public String addProduct(){
        // 插入100000
        long start = System.currentTimeMillis();
        // 获取SqlSession，并开启批量执行模式
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
        try {
            ProductMapper productMapper = session.getMapper(ProductMapper.class);
            for (int i = 0; i < 500000; i++) {
                Product product = new Product();
                product.setId(IdUtil.simpleUUID());
                product.setName("product" + i);
                product.setRate(new BigDecimal(i));
                product.setAmount(new BigDecimal(i));
                product.setRaised(new BigDecimal(i));
                product.setCycle(i);
                product.setEndTime("2024-09-24");
                productMapper.insert(product);
                if (i == 99) {
                    throw new RuntimeException("报错啦");
                }
            }
            System.out.println("开始批量提交commit");
            session.commit();
        } catch (Exception e) {
            // 事务回滚
            session.rollback();
            e.printStackTrace();
            return "插入失败";
        } finally {
            // 清除缓存, 关闭session
            session.clearCache();
            session.close();
        }
        long end = System.currentTimeMillis();

        // 打印用时多少秒
        System.out.println("用时：" + (end - start) / 1000 + "秒");

        return "批量插入完成";
    }

    /**
     * 单条插入
     *
     * @return
     */
    @GetMapping("/add/product2")
    public String addProduct2(){
        // 插入100000
        long start = System.currentTimeMillis();
        for (int i = 0; i < 500000; i++) {
            Product product = new Product();
            product.setId(IdUtil.simpleUUID());
            product.setName("product" + i);
            product.setRate(new BigDecimal(i));
            product.setAmount(new BigDecimal(i));
            product.setRaised(new BigDecimal(i));
            product.setCycle(i);
            product.setEndTime("2024-09-24");
            productMapper.insert(product);
        }
        long end = System.currentTimeMillis();

        // 打印用时多少秒
        System.out.println("用时：" + (end - start) / 1000 + "秒");

        return "单条插入完成";
    }

}
